package com.ymatou.shoppingcart.api.testcase;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.shoppingcart.api.parameter.ChangeCatalogAllSelectedBean;
import com.ymatou.shoppingcart.api.service.ChangeCatalogAllSelectedCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.ShoppingCartCallServices;
import com.ymttest.database.mongo.ShopCartMongoBean;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 修改全部购物车选中状态 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeCatalogAllSelected {
	private static ChangeCatalogAllSelectedBean changeallbean;
	private static ChangeCatalogAllSelectedCall changeallcall;
	ShoppingCartMongoWapper scw;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改全部购物车选中状态");
	}

	@Before
	public void caseUp() {
		changeallbean = new ChangeCatalogAllSelectedBean();
		changeallcall = new ChangeCatalogAllSelectedCall();
		try {
			scw = new ShoppingCartMongoWapper();
		} catch (Exception e) {
			Logger.setup("MongoDB初始化失败：" + e.getMessage(), -1);
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeallcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	//TODO 购物车表和结构都有修改，case需要对应调整
	/*@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeCatalogAllSelected_001() {
		Logger.start(true,"修改全部购物车选中状态成功,修改为未选中状态");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setUserId:" + re.getUid());
			changeallbean.setUserId(re.getUid());
			Logger.comment("setIsChecked:" + false);
			changeallbean.setIsChecked("false");
			changeallcall.setData(changeallbean);
			changeallcall.callService();
			Logger.comment("changeallcall send:" + changeallcall.getEntityString());
			Logger.comment("changeallcall return:" + changeallcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changeallcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changeallcall.getBCode(), "验证返回bcode");
			ArrayList<ShopCartMongoBean> list = scw.getShoppingCartInfoList("{\"uid\":\"" + re.getUid() + "\"}", 0);
			for (ShopCartMongoBean scb : list) {
				Logger.verifyEquals("false", scb.getCheck(), scb.get_id() + "验证Check状态");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCatalogAllSelected_002() {
		Logger.start(true,"输入SellerId，修改这个SellerId对应商品的选中状态成功");
		try {
			// 这里可能出现商品都是wesper发布的情况

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 5);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			Logger.comment("从mongodb数据库获取一条uid为'" + uid + "'的购物车记录:" + num);
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);

			Logger.comment("setUserId:" + cartbean.getUid());
			changeallbean.setUserId(cartbean.getUid());
			Logger.comment("setIsChecked:" + false);
			changeallbean.setIsChecked("false");
			Logger.comment("setSellerId:" + cartbean.getSid());
			changeallbean.setSellerId(cartbean.getSid());
			changeallcall.setData(changeallbean);
			changeallcall.callService();
			Logger.comment("changeallcall send:" + changeallcall.getEntityString());
			Logger.comment("changeallcall return:" + changeallcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changeallcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changeallcall.getBCode(), "验证返回bcode");
			ArrayList<ShopCartMongoBean> list = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 0);
			for (ShopCartMongoBean scb : list) {
				if (scb.getSid().equals(cartbean.getSid())) {
					Logger.verifyEquals("false", scb.getCheck(), scb.get_id() + "验证Check状态");
				} else {
					Logger.verifyEquals("true", scb.getCheck(), scb.get_id() + "验证Check状态");
				}
			}

		} catch (Exception e) {
			Logger.fail(e);
		}

	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCatalogAllSelected_003() {
		Logger.start(true,"不输入必填项UserId，修改全部购物车选中状态失败");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			// Logger.comment("setUserId:"+re.getUid());
			// changeallbean.setUserId(re.getUid());
			Logger.comment("setIsChecked:" + false);
			changeallbean.setIsChecked("false");
			changeallcall.setData(changeallbean);
			changeallcall.callService();
			Logger.comment("changeallcall send:" + changeallcall.getEntityString());
			Logger.comment("changeallcall return:" + changeallcall.getReturnData());
			// 这里错误编码和信息重复
			Logger.verifyEquals(ECode.ERROR500, changeallcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changeallcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changeallcall.getMsg().contains(ECode.ERROR500_MSG_2), "验证返回Msg是否包含字符:"
					+ ECode.ERROR500_MSG_2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCatalogAllSelected_004() {
		Logger.start(true,"输入错误SellerId(-1)，修改全部购物车选中状态失败");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setUserId:" + re.getUid());
			changeallbean.setUserId(re.getUid());
			Logger.comment("setIsChecked:" + false);
			changeallbean.setIsChecked("false");
			Logger.comment("setSellerId:-1");
			changeallbean.setSellerId("-1");
			changeallcall.setData(changeallbean);
			changeallcall.callService();
			Logger.comment("changeallcall send:" + changeallcall.getEntityString());
			Logger.comment("changeallcall return:" + changeallcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR500, changeallcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changeallcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changeallcall.getMsg().contains(ECode.ERROR500_MSG_3), "验证返回Msg是否包含字符:"
					+ ECode.ERROR500_MSG_3);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCatalogAllSelected_005() {
		// IsChecked需要外部系统保证正确传入，购物车接口本身不做判断.如不传入默认false
		Logger.start(true,"不输入选中状态IsChecked，修改全部购物车选中状态成功");
		try {
			Logger.comment("从mongodb中随机获取一条购物车记录");

			ShopCartMongoBean re = scw.getRandomShoppingCartInfo();
			Logger.comment("setUserId:" + re.getUid());
			changeallbean.setUserId(re.getUid());
			// Logger.comment("setIsChecked:"+false);
			// changeallbean.setIsChecked("false");
			changeallcall.setData(changeallbean);
			changeallcall.callService();
			Logger.comment("changeallcall send:" + changeallcall.getEntityString());
			Logger.comment("changeallcall return:" + changeallcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changeallcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changeallcall.getBCode(), "验证返回bcode");
			ArrayList<ShopCartMongoBean> list = scw.getShoppingCartInfoList("{\"uid\":\"" + re.getUid() + "\"}", 0);
			for (ShopCartMongoBean scb : list) {
				Logger.verifyEquals("false", scb.getCheck(), scb.get_id() + "验证Check状态");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}*/
}